package classpath

import (
	"os"
	"path/filepath"
)
type Classpath struct{
	//启动类路径
	bootClasspath Entry
	//扩展类路径
	extClasspath Entry
	//用户类路径
	userClasspath Entry
}

func Parse(jreOption , cpOption string)  *Classpath {
	cp := &Classpath{}
	cp.parseBootAndExtClasspath(jreOption)
	cp.parseUserClasspath(cpOption)
	return cp ;
}
func (self *Classpath)parseBootAndExtClasspath(jreOption string)  {
	jreDir := getJreDir(jreOption)
	//jre/lib/*
	jreLibPath := filepath.Join(jreDir,"lib","*")
	self.bootClasspath = newWildcardEntry(jreLibPath)
	jreExtPath := filepath.Join(jreDir,"lib","ext","*")
	self.extClasspath = newWildcardEntry(jreExtPath)
	return 
}

func getJreDir(jreOption string) string {
	if jreOption != "" && exists(jreOption){
		return jreOption
	}
	if exists("./jre"){
		return "./jre"
	}
	if jh := os.Getenv("JAVA_HOME"); jh != ""{
		return filepath.Join(jh,"jre")
	}
	panic("Can not find jre folder")
	
}

//判断路径是否存在
func exists(path string) bool{
	if _,err := os.Stat(path); err == nil {
		if os.IsNotExist(err){
			return false
		}
	}
	return true
}

func (self *Classpath)parseUserClasspath(cpOpting string)  {
	if cpOpting == ""{
		cpOpting = "."
	}
	self.userClasspath = newEnry(cpOpting)
	
}
func (self *Classpath)ReadClass(className string)  ([]byte,Entry,error){
	className = className + ".class"
	if data,entry ,err := self.bootClasspath.readClass(className); err == nil {
		return data,entry,err
	}
	if data,entry,err := self.extClasspath.readClass(className); err == nil {
		return data,entry,err
	}
	return self.userClasspath.readClass(className)
}

func (self *Classpath) String() string {
	return self.userClasspath.String()
}